package com.zhku.shoppingspringboot.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果，服务端响应的数据最终都会封装成此对象
 * @param <T>
 */
@Data
@ApiModel("返回结果")
public class R<T> implements Serializable {

    @ApiModelProperty( value ="编码")
    private int code;
    @ApiModelProperty("信息")
    private String msg;
    @ApiModelProperty("数据")
    private T data; //数据
    @ApiModelProperty("动态数据")
    private Map<String,Object> map = new HashMap(); //动态数据
    public static  R success() {
        R r = new R();
        r.code=ResultCode.SUCCESS.getCode();
        r.msg=ResultCode.SUCCESS.getDefauleMessage();
        return r;
    }
    public static  R success(String msg) {
        R r = new R();
        r.code=ResultCode.SUCCESS.getCode();
        r.msg=msg;
        return r;
    }
    public static <T> R<T> success(String msg,T object) {
        R<T> r = new R<T>();
        r.code=ResultCode.SUCCESS.getCode();
        r.msg=msg;
        r.data = object;
        return r;
    }
    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.code=ResultCode.SUCCESS.getCode();
        r.msg=ResultCode.SUCCESS.getDefauleMessage();
        r.data = object;
        return r;
    }

    public static  R error(ResultCode resultCode) {
        R r = new R();
        r.code=resultCode.getCode();
        r.msg=resultCode.getDefauleMessage();
        return r;
    }
    public static  R error(ResultCode resultCode,String message) {
        R r = new R();
        r.code=resultCode.getCode();
        r.msg=message;
        return r;
    }

    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}
